let mCurPage = 1
let mMaxLimit = 3

$(function() {
    let dog = new $.dog();

    // 按钮提交评论事件
    $("#submit").bind("click", onBtnClickSubmit)

    // 按钮加载评论事件
    $("#more-comment-btn").bind("click", function() { loadComment(mCurPage, mMaxLimit) })

    $("#more-comment-btn").hide()
    $("#not-more-comment-btn").hide()

    loadComment(mCurPage, mMaxLimit);

    function loadComment(pageNum, pageLimit) {

        $('.mdx-comments-loading').fadeIn(200);
        $("#more-comment-btn").hide()

        dog.get({
            url: '/api/v1/comment/',
            data: {
                pageNum: pageNum,
                pageLimit: pageLimit,
                postId: $('#postId').val()
            },
            success: function(result) {
                $('.mdx-comments-loading').hide();
                if (result.errCode != 0) {
                    mdui.snackbar({
                        message: result.errDesc + "(" + result.errCode + ")",
                        timeout: 5000,
                        position: 'top',
                    });
                    return
                }
                loadCommentSuccess(result.data)
            },
            error: function(error) {
                $('.mdx-comments-loading').hide();
                mdui.snackbar({
                    message: error || '数据加载失败',
                    timeout: 5000,
                    position: 'top',
                });
            }
        });
    }

    function onBtnClickSubmit() {
        var name = $('#username').val();
        if (isEmpty(name)) {
            mdui.snackbar({ message: "请输入用户名", timeout: 3000, position: 'top', });
            return
        }

        var email = $('#email').val();
        if (isEmpty(email)) {
            mdui.snackbar({ message: "请输入邮箱", timeout: 3000, position: 'top', });
            return
        }

        var comment = $('#comment').val();
        if (isEmpty(comment)) {
            mdui.snackbar({ message: "请输入评论内容", timeout: 3000, position: 'top', });
            return
        }

        var url = $('#url').val();
        if (!isEmpty(url) && !dog.checkURL(url)) {
            mdui.snackbar({ message: "URL错误", timeout: 3000, position: 'top', });
            return
        }

        dog.post({
            url: '/api/v1/comment/',
            data: $('#commentform').serialize(),
            success: function(result) {
                if (result.errCode != 0) {
                    mdui.snackbar({
                        message: result.errDesc + "(" + result.errCode + ")",
                        timeout: 5000,
                        position: 'top',
                    });
                    return
                }
                addComment({ UserName: name, Email: email, Content: comment, URL: url, Time: dog.dateFormat("YYYY-mm-dd", new Date()) }, true)
                cleanComment();
            },
            error: function(error) {
                mdui.snackbar({
                    message: error || '数据加载失败',
                    timeout: 5000,
                    position: 'top',
                });
            }
        });
    }

    function cleanComment(){
        $('#username').val("");
        $('#comment').val("");
        $('#email').val("");
        $('#url').val("");
    }

    function isEmpty(obj) {
        if (typeof obj == "undefined" || obj == null || obj == "") {
            return true;
        } else {
            return false;
        }
    }

    function loadCommentSuccess(data) {
        mCurPage = data.nextPage;
        let hasNextPage = data.hasNextPage;
        let rows = data.rows;

        rows.forEach(function(comment) {
            addComment(comment, false)
        })

        if (hasNextPage) {
            $("#more-comment-btn").show()
        } else {
            $("#more-comment-btn").hide()
            $("#not-more-comment-btn").show()
        }
    }

    /**
     * 添加一条评论
     * @param {CommentModel} data 评论数据
     * @param {*} pre  是否添加到头
     */
    function addComment(data, pre) {
        let html = '<li class="mdui-list-item" id="li-comment-' + data.Id + '">' +
            '<div class="mdui-list-item-avatar">' +
            '<img width="120" height="120" class="avatar avatar-120 photo lazyload mdx-avatar-lazyload" src="" data-src="/static/comm/image/avatar/avator' + dog.random(30, 2) + '.png" alt="" data-srcset="">' +
            '</div>' +
            '<div class="mdui-list-item-content outbu" id="' + data.Id + '">' +
            '<div class="mdui-list-item-title">' +
            '<a href=' + data.URL + ' target="_blank">' + data.UserName + '</a>' +
            '</div>' +
            '<div class="mdui-list-item-text mdui-typo">' +
            '<p>' + data.Content + '</p>' +
            '</div>' +
            '<span class="mdx-reply-time">' + data.Time + '</span>' +
            '</div>' +
            '</li>' +
            '<li class="mdui-divider-inset mdui-m-y-0"></li>';

        if (pre)
            $(".ajax-comments").prepend(html);
        else
            $(".ajax-comments").append(html);
    }
})